package com.android.pagemarket.ui.activity.base;

import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.provider.Settings;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.ViewStub;
import android.widget.LinearLayout;
import android.widget.ProgressBar;

import com.android.pagemarket.MyApplication;
import com.android.pagemarket.R;
import com.android.pagemarket.ui.widget.LoadableView.IdisplayNetErrAndRefreshView;
import com.android.pagemarket.ui.widget.TitleBar;

/*******************************************************
 * @author : zhaohua
 * @version: 2012-08-14
 * @see:     继承自这个Activity的界面有统一的标题栏
 * @Copyright: copyrights reserved by personal 2007-2012
*******************************************************/
public class TemplateActivity extends BaseActivity implements IdisplayNetErrAndRefreshView
{
    // 传递参数
    public static final String KEY_PARAM = "_param";
	/**标题栏 */
	protected TitleBar mTitleBar;
	/**内容容器*/
	private LinearLayout mContainer;
	/** 底部栏*/
    protected LinearLayout mBottomBar;
	/** 广告栏*/
	private ViewStub advStub;
	/** 中间的转菊花*/
	protected ProgressBar mLoading;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume()
	{	    
	    super.onResume();
	    showAdvView(false);
	    checkNetworkAvalid();
	}
	
	public void setContentView(View view)
	{
		initLayout(view);
	}
	
	@Override
	public void setContentView(int layoutResID)
	{
		View content = getLayoutInflater().inflate(layoutResID, null);
		initLayout(content);
	}

	private void initLayout(View content) 
	{
		// 设置模板
		super.setContentView(R.layout.activity_template);
		// 初始化模拟成员
		initComponents();		
		// 将内容放入容器		
		mContainer.addView(content,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
		// 设置模板的标题栏
		setupTitleBar(mTitleBar);
		// 设置模板的底部栏
		setupBottomBar(mBottomBar);
	}
	
	/**
	 * 初始化三大块：头部的标题栏，中间的内容容器，底部的工具栏
	 */
	private void initComponents()
	{
	    mTitleBar = (TitleBar)findViewById(R.id.titleBar);	
	    mContainer = (LinearLayout)findViewById(R.id.container);
	    mBottomBar = (LinearLayout)findViewById(R.id.bottomBar);
	    mLoading = getViewById(R.id.loading);
	    showAdvView(false);
	}
	
	/** 初始化广告组件*/
	private void initAdvViews()
	{
	    
	}
	
	/** 是否显示广告视图*/
	private void showAdvView(boolean visible)
	{
	    advStub = (ViewStub)findViewById(R.id.advStub);
	    if(advStub == null)
	    {
	        // 已经初始化了
	    }else
	    {
	        // 还没有，只有显示时，才初始化
	        if(visible)
	        {
	            advStub.setVisibility(View.VISIBLE);
	            initAdvViews();
	        }
	    }
	    if(visible)
	    {
	        
	    }
	}
	
	/** 设置标题栏*/
	protected void setupTitleBar(TitleBar titleBar)
	{
	    titleBar.setRightVisible(false);
	}

	/** 设置底部栏*/
    protected void setupBottomBar(LinearLayout bottomBar){}
    
    /** 显示转菊花*/
    public void showLoading(boolean visible)
    {
        mLoading.setVisibility(visible ? View.VISIBLE : View.GONE);
    }
    
    @Override
    public void refreshView()
    {
        
    }

    @Override
    public void showNoNetworkDialog()
    {
        showDialog("网络设置提示", "网络连接不可用,是否进行设置?", "取消", "设置", 
                new DialogInterface.OnClickListener()
        {           
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
              startActivity(new Intent(Settings.ACTION_WIRELESS_SETTINGS));
            }
        });
    }
    
    private void checkNetworkAvalid()
    {
        if( !MyApplication.getContext().isNetWorkAvailable())
        {
            showNoNetworkDialog();
        }
    }
}